package com.oron3.lock;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class IOUtils {
	public static final String LINE_SEPRATOR = System.getProperty("line.separator");
	public static byte[] getBytes(InputStream is) throws IOException {
		BufferedInputStream bis =new BufferedInputStream(is);
		ByteArrayOutputStream bas=new ByteArrayOutputStream();
		int fByte;
		while ((fByte = bis.read()) != -1) 
			bas.write(fByte);
		bis.close();
		bas.flush();
		byte[] baf= bas.toByteArray();
		bas.close();
		return baf;
	}
	public static InputStream getZipEntry(InputStream input,String entryName) {
		ZipInputStream zis = new ZipInputStream(new BufferedInputStream(input));
		try {
			ZipEntry entry;
			while ((entry = zis.getNextEntry())!=null && !entryName.equalsIgnoreCase(entry.getName()));
			return zis;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static String readLine(InputStream inputStream) throws IOException {
		BufferedReader bf = null;
		String line;
		try {
			bf =new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
			line = bf.readLine();
		} catch(IOException  e) {
			throw e;
		} finally {
			if (bf!=null) bf.close();
		}
		return line;
	}
	public static String readLine(File data) throws FileNotFoundException, IOException {
		return readLine(new FileInputStream(data));
	}
	
	public static String readAll(File data) throws FileNotFoundException, IOException {
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(data),"UTF-8"));
			StringBuilder sb = new StringBuilder();
			char[] readBlock = new char[8192]; 
			int res;
			while  ((res=bufferedReader.read(readBlock)) != -1) {
				sb.append(readBlock,0,res);
			}
			return sb.toString();
		} catch(IOException e) {
			throw e; 
		} finally {
			if (bufferedReader!=null)
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
				
	}
	public static String readAll(InputStream data) throws FileNotFoundException, IOException {
		BufferedInputStream bufferedReader = null;
		try {
			bufferedReader = new BufferedInputStream(data);
			StringBuilder sb = new StringBuilder();
			byte[] readBlock = new byte[8192]; 
			int res;
			while  ((res=bufferedReader.read(readBlock)) != -1) {
				sb.append(readBlock);
			}
			return sb.toString();
		} catch(IOException e) {
			throw e; 
		} finally {
			if (bufferedReader!=null)
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
				
	}
	public static byte[] getBytes(File file) throws FileNotFoundException, IOException {
		return getBytes(new BufferedInputStream(new FileInputStream(file)));
	}
	public static String readAll(BufferedReader reader) throws IOException {
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(reader);
			StringBuilder sb = new StringBuilder();
			String res;
			while  ((res=bufferedReader.readLine()) != null) {
				sb.append(res);
			}
			return sb.toString();
		} catch(IOException e) {
			throw e; 
		} finally {
			if (bufferedReader!=null)
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}



}
